package com.aididi.concurrent;

import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

/**
 * @Auther: xuzexiang
 * @Date: 2018-11-5 0005 21:38
 * @Description:    对象数据字段更新器
 */
public class AtomicReferenceFieldUpdaterTest {
    public static void main(String[] args) {
       /* Student student = new Student("yinwenjie" , 80);
        AtomicReference<Student> ref = new AtomicReference<Student>(student);

        student = new Student("yinwenjie" , 70);
        Student oldStudent = ref.getAndSet(student);

        System.out.println(student + "和" + oldStudent + "是两个对象");
        System.out.println("AtomicReference保证了赋值时的原子操作性");*/

        Student student = new Student();
        Performance newPerformance = new Performance();
        newPerformance.setPerformance(80);
        // 注意，这样student中的performance属性
        // 就是用了乐观机制，保证了操作的线程安全性
        student.setPerformance(newPerformance);

        // 再设置一次
        Performance otherPerformance = new Performance();
        otherPerformance.setPerformance(100);
        student.setPerformance(otherPerformance);
        System.out.println("student还是一个");
        System.out.println(newPerformance + "和" + otherPerformance + "不一样了");

    }

}


/**
 * 代表学生的Student类
 * @author yinwenjie
 */
class Student {
    /**
     * 学生成绩
     */
    private volatile Performance performance;
    /**
     * 学生姓名
     */
    private String name;

   /* public Student(String name , Integer performance) {
        this.name = name;
        this.performance = new Performance();
        this.performance.setPerformance(performance);
    }*/
    /**
     * 学生成绩“更改者”
     */
    // 会重点讲解关于“更改器”的参数问题
    private  AtomicReferenceFieldUpdater<Student, Performance> performance_updater =
            AtomicReferenceFieldUpdater.newUpdater(Student.class, Performance.class, "performance");


    /**
     * @return the performance
     */
    public Performance getPerformance() {
        return performance;
    }
    /**
     * @param performance the performance to set
     */
  /*  public void setPerformance(Performance performance) {
        this.performance = performance;
    }*/

    /**
     * @param performance the performance to set
     */
    public void setPerformance(Performance performance) {
        // 注意，这里设置的是updater，而不是直接设置performance属性
        performance_updater.set(this, performance);
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
}

/**
 * 代表着学生成绩
 * @author yinwenjie
 */
class Performance {
    /**
     * 成绩属性是一个整数
     */
    private Integer performance;

    /**
     * @return the performance
     */
    public Integer getPerformance() {
        return performance;
    }

    /**
     * @param performance the performance to set
     */
    public void setPerformance(Integer performance) {
        this.performance = performance;
    }
}